package com.app.controller;

import com.app.common.Result;
import com.app.model.*;
import com.app.server.*;
import com.app.utils.CommonHelp;
import com.app.utils.TokenUtils;
import com.app.vo.DisasterHandleVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 报灾处理
 */
@Slf4j
@RestController
@RequestMapping("/sbbz/handle")
public class DisasterHandleController {
    @Autowired
    private AdminUserService adminUserService;
    @Autowired
    private DisasterHandleService disasterHandleService;
    @Autowired
    private DisasterReportingService disasterReportingService;
    @Autowired
    private RecordFileService recordFileService;
    @Autowired
    private UnitsManagerService unitsManagerService;

    // 查询列表
    @GetMapping("/getHandleList")
    public Result getHandleList(@RequestParam(defaultValue = "0") Integer page,
                                @RequestParam(defaultValue = "10") Integer limit, Integer disasterId, String keyword) {
        DisasterHandleVo disasterHandleList = disasterHandleService.getDisasterHandleList(page, limit, disasterId, keyword);
        return Result.success(disasterHandleList);
    }

    //根据id查询
    @GetMapping("/getHandleById")
    public Result getHandleById(Integer id, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        DisasterHandleModel disasterHandle = disasterHandleService.getById(id);
        if (disasterHandle == null) {
            return Result.success("数据不存在");
        }
        List<String> urlList = new ArrayList<>();
        List<String> videoList = new ArrayList<>();
        String fileIdArr = disasterHandle.getFileId();
        String[] split = fileIdArr.split(",");
        for (String fileIds : split) {
            int fileId = Integer.parseInt(fileIds);
            RecordFileModel recordFile = recordFileService.getById(fileId);
            if (recordFile != null) {
                if (recordFile.getFileType() != null && recordFile.getFileType() == 1) {
                    urlList.add(recordFile.getUrl());
                }
                if (recordFile.getFileType() != null &&recordFile.getFileType() == 2) {
                    videoList.add(recordFile.getUrl());
                }
            }
        }
        disasterHandle.setUrlList(urlList);
        disasterHandle.setVideoList(videoList);
        return Result.success(disasterHandle);
    }

    // 新增和修改
    @PostMapping("/addAndUpdateHandle")
    public Result addAndUpdateHandle(@RequestBody DisasterHandleModel disasterHandleModel, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String adminId = TokenUtils.verify(token);
        if (adminId.equals("false")) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel adminUser = adminUserService.getById(Integer.parseInt(adminId));
        if (adminUser == null) {
            return Result.error(406, "用户不存在");
        }
        //新增和修改报灾处理
        Result<String> stringResult = disasterHandleService.addAndUpdateHandle(disasterHandleModel);
        //处理完成后 更改状态
        DisasterReportingModel disasterReporting = disasterReportingService.getById(disasterHandleModel.getDisasterId());
        disasterReporting.setAllocationStatus(3);
        disasterReporting.setHandleStatus(2);
        if (adminUser.getUnitsId() != null) {
            UnitsManagerModel byId = unitsManagerService.getById(adminUser.getUnitsId());
            if (byId != null) {
                disasterReporting.setRealName(byId.getName());
            }
        }
        disasterReportingService.updateById(disasterReporting);
        return stringResult;
    }

    //删除
    @GetMapping("/delHandle")
    public Result delHandle(Integer id) {
        disasterHandleService.removeById(id);
        return Result.success("删除成功");
    }
}
